//
//  PdViewController.m
//  Koala_Fm
//
//  Created by dllo on 16/2/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "PdViewController.h"
#import "PdTableViewCell.h"
#import "AFNetBlock.h"
#import "PdModel.h"
#import <UIImageView+WebCache.h>
#import "TopicsModel.h"
#import "PlayViewController.h"
@interface PdViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic, retain)UITableView *tableView;
@property(nonatomic, retain)NSMutableArray *dataArr;
@property(nonatomic, retain)UIView *header;
@property(nonatomic, retain)UIImageView *image;
@property(nonatomic, retain)NSString *string;
@property(nonatomic, retain)UILabel *lable;
@property(nonatomic, retain)UILabel *splendor;
@end

@implementation PdViewController

- (void)dealloc
{
    [_tableView release];
    [_dataArr release];
    [_header release];
    [_image release];
    [_string release];
    [_lable release];
    [_splendor release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    /** tableView设置 **/
    self.view.backgroundColor =[UIColor whiteColor];
    self.navigationItem.title = _model.name;
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:_tableView];
    [_tableView release];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    /** 重用池**/
    [_tableView registerClass:[PdTableViewCell class] forCellReuseIdentifier:@"cell"];
    /**数据处理**/
    NSString *str = @"http://m.kaolafm.com/apiv4/topic/info?id=";
    NSString *strr = _model.id;
    NSString *string =[NSString stringWithFormat:@"%@%@", str, strr];
    [AFNetBlock GetDataWithURL:string block:^(id block) {
        NSArray *array = block[@"result"][@"dataList"];
        self.string = block[@"result"][@"introduction"];
        /** 调用文字高度方法 **/
        CGFloat h = [PdTableViewCell  heightWithText:_string];
        /** 添加头部区域 **/
        self.header = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 + h + 40)];
//        _header.backgroundColor = [UIColor redColor];
        _tableView.tableHeaderView = _header;
        
        self.image = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
        _image.backgroundColor = [UIColor blackColor];
        [_header addSubview:_image];
        [_image release];
        [_image sd_setImageWithURL:[NSURL URLWithString:_model.img]];
    
        self.lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 200, self.view.frame.size.width, h)];
//        self.lable.backgroundColor = [UIColor cyanColor];
        self.lable.numberOfLines = 0;
        self.lable.text = _string;
        [_header addSubview:_lable];
        [_lable release];
        self.splendor = [[UILabel alloc]initWithFrame:CGRectMake(10, 200 + h + 10, self.view.frame.size.width, 30)];
//        self.splendor.backgroundColor = [UIColor blueColor];
        self.splendor.text = @"相关推荐";
        [_header addSubview:_splendor];
        [_splendor release];
        self.dataArr = [PdModel modelHandlerWithArray:array];
        //   NSLog(@"%@", _dateArr);
        
        // 刷新页面
        
        [_tableView reloadData];
        
    }];
    

    
}
#pragma mark - tableView协议设置

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PdTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    PdModel *m = _dataArr[indexPath.row];
    cell.name.text = m.name;
    [cell.smallImage sd_setImageWithURL:[NSURL URLWithString:m.img]];
    
    return cell;
}

// 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return 90;
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
